//
//  RadioViewController.m
//  Leisure
//
//  Created by 大展 on 16/6/30.
//  Copyright © 2016年 大展. All rights reserved.
//

#import "RadioViewController.h"

@interface RadioViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *scrollArray;
@property (nonatomic, strong)NSMutableArray *dataSource;
@property (nonatomic, assign)NSInteger start;
@property (nonatomic, assign)NSInteger limit;

@end

@implementation RadioViewController

- (NSMutableArray *)dataSource {

    if (!_dataSource) {

        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSMutableArray *)scrollArray {

    if (!_scrollArray) {

        _scrollArray = [NSMutableArray array];
    }
    return _scrollArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];


    [self.tableView registerNib:[UINib nibWithNibName:@"RadioCell" bundle:nil] forCellReuseIdentifier:@"RadioModel_cell"];

    // 数据请求
    [RequestManager fecthDataWithUrl:kRadioUrl parameter:@{} mehtod:@"POST" compelet:^(NSData *data) {

        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",bigDic);
        for (NSDictionary *smallDic in bigDic[@"data"][@"carousel"]) {

            [self.scrollArray addObject:smallDic[@"img"]];
        }
        for (NSDictionary *smallDic in bigDic[@"data"][@"alllist"]) {

            RadioModel *model = [[RadioModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.dataSource addObject:model];
        }

    } updateUI:^{

        [self.tableView reloadData];
        // 创建轮播图
        ZZScrollView *headerView = [[ZZScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/3) imageArray:self.scrollArray];
        self.tableView.tableHeaderView = headerView;
    }];

    self.tableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{


        self.start += 10;
        [RequestManager fecthDataWithUrl:kRadioMoreUrl parameter:@{@"limit":@(self.limit),@"start":@(self.start)} mehtod:@"POST" compelet:^(NSData *data) {

            NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

            NSLog(@"%@",bigDic);
            for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {

                RadioModel *model = [[RadioModel alloc]init];
                [model setValuesForKeysWithDictionary:smallDic];
                [self.dataSource addObject:model];
            }

        } updateUI:^{
            [self.tableView.mj_footer endRefreshing];
            [self.tableView reloadData];

        }];
    }];
}

#pragma mark ----tableViewDelegate----
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    BaseCell *cell = [FactoryMode productCellWithModel:self.dataSource[indexPath.row] tableView:tableView indexPath:indexPath];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    RadioDetailViewController *radioDetailViewController = [[RadioDetailViewController alloc]initWithNibName:@"RadioDetailViewController" bundle:nil];

    radioDetailViewController.radioid = [self.dataSource[indexPath.row] radioid];

    [self.navigationController pushViewController:radioDetailViewController animated:YES];
    

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 200;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
